package javabean;

import java.io.*;

public class Userbean {
    private String username;
    private String surname;
    private String name;
    private String email;
    private String phone;
    private String password;
    private String confirmpassword;
    private String image;
    private String count;
    public Userbean() {
    }

    public Userbean(String username,String password,String email,String phone,String confirmpassword)
    {
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.confirmpassword = confirmpassword;
    }

    public Userbean(String username,String password)
    {
        this.username = username;
        this.password = password;
    }

    public Userbean(String username,String password,String surname,String name)
    {
        this.username = username;
        this.password = password;
        this.surname = surname;
        this.name = name;
    }

    public Userbean(String username,String surname,String name,String email,String phone,String confirmpassword)
    {
        this.username = username;
        this.surname = surname;
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.confirmpassword = confirmpassword;
    }

    public boolean exist() throws IOException {
        BufferedReader in  = new BufferedReader(new FileReader("Resource/message.txt"));
        String message;
        boolean flags = false;
        while((message=in.readLine())!=null)
        {
            String user[] = message.split("\\$");
            if(user[0].equals(username)) {
                flags = true;
                break;
            }
        }
        in.close();
        return flags;
    }

    public boolean save() throws IOException {
        FileWriter out = new FileWriter("Resource/message.txt",true);
        out.write(username+"$"+password+"$"+surname+"$"+name+"$"+phone+"$"+email+"\n");
        out.close();
        return  true;
    }

    public boolean login() throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("Resource/message.txt"));
        String message;
        boolean flags = false;
        while((message=in.readLine())!=null)
        {
            String user[] = message.split("\\$");
            if(user[0].equals(username)&&user[1].equals(password)) {
                flags = true;
                break;
            }
        }
        in.close();
        return flags;
    }

    public void change() throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("Resource/message.txt"));
        String message="";

        String user;
        while((user=in.readLine())!=null)
        {
            String users[] = user.split("\\$");
            if(users[0].equals(username)) {
                user = username + "$" + users[1] + "$" + surname + "$" + name + "$" + phone + "$" + email;
            }
            message = message+user+"\n";
        }
        in.close();
        FileWriter out = new FileWriter("Resource/message.txt");
        out.write(message);
        out.close();

    }
    public void show() throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("Resource/message.txt"));
        String message;

        while((message=in.readLine())!=null)
        {
            String user[] = message.split("\\$");
            if(user[0].equals(username)) {
                this.username = user[0];
                this.password = user[1];
                this.surname = user[2];
                this.name = user[3];
                this.phone = user[4];
                this.email = user[5];
                break;
            }
        }
        in.close();
    }

    public void changeimage()throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("Resource/image.txt"));
        String message="";

        int i=0,j=1;
        String user;
        while((user=in.readLine())!=null)
        {
            String users[] = user.split("\\$");
            if(users[0].equals(username))
                user = username+"$"+image+"$"+count;
            message = message+user+"\n";
            i++;
        }
        in.close();
        FileWriter out = new FileWriter("Resource/image.txt");
        out.write(message);
        out.close();
    }
    public void saveimage()throws IOException
    {
        FileWriter out = new FileWriter("Resource/image.txt",true);
        out.write(username+"$"+image+"$"+count+"\n");
        out.close();
    }
    public boolean imageexist()throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("Resource/image.txt"));
        String message;
        boolean flags=false;
        while((message=in.readLine())!=null)
        {
            String user[] = message.split("\\$");
            if(user[0].equals(username))
            {
                this.image = user[1];
                this.count = user[2];
                flags = true;
                break;
            }
        }
        if(!flags)
        {
            this.image = "dgut.jpg";
            this.count = "0";
        }
        in.close();
        return flags;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getConfirmpassword() {
        return confirmpassword;
    }

    public void setConfirmpassword(String confirmpassword) {
        this.confirmpassword = confirmpassword;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }
}
